package com.topvision.controller.glass;

import java.util.List;
import java.util.Map;

import com.topvision.controller.AbstractController;
import com.topvision.model.glass.Message;
import com.topvision.util.ConstantsUtil;
import com.topvision.utils.glass.JpushUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topvision.service.glass.MessageService;
import com.topvision.util.PageUtils;
import com.topvision.util.Query;
import com.topvision.util.R;

/**
 * 消息推送模块
 *
 * @author jack
 * @email 791127866@qq.com
 * @date 2017-08-08 13:28:14
 */
@RestController
@RequestMapping("glass/message")
public class MessageController extends AbstractController {
    @Autowired
    private MessageService messageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("glass/message:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<Message> messageList = messageService.queryList(query);
        int total = messageService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(messageList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("glass/message:info")
    public R info(@PathVariable("id") Integer id) {
        Message message = messageService.queryObject(id);
        return R.success().put("message", message);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("glass/message:save")
    public R save(@RequestBody Message message) {
        messageService.save(message);
        return R.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("glass/message:update")
    public R update(@RequestBody Message message) {
        messageService.update(message);
        return R.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("glass/message:delete")
    public R delete(@RequestBody Integer[] ids) {
        messageService.deleteBatch(ids);
        return R.success();
    }


    @RequestMapping(value = "/send")
    public R customNotification(@RequestBody Message message) throws Exception {
        R saveResult = save(message);
        int resultCode = (int) saveResult.get("code");
        if (resultCode == ConstantsUtil.CommonCode.SUCCESS_CODE) {
            // 保存信息到数据库中
            Map<String, Object> resultMap = JpushUtil.customNotification(message);
            if ((boolean) resultMap.get("success")) {
                long msgId = (long) resultMap.get("msgId");
                message.setMsgId(String.valueOf(msgId));
                return update(message);
            } else {
                return R.error((int) resultMap.get("code"), (String) resultMap.get("info"));
            }
        } else {
            return R.error("数据库异常,保存消息失败...");
        }
    }

}
